const router = require('koa-router')();
router.prefix(appConfig.authPrefixUrl+'/desktop/sys_role');
const SysRoleService = appRequire('service/desktop/SysRoleService.js');
const SysAuthService = appRequire('service/desktop/SysAuthService.js');
const ResultVo = appRequire('vo/ResultVo.js');

router.get('/index',async(ctx)=>{
  ctx.body="sys_role 你好";
});
router.post('/save',async(ctx)=>{
  try{
    let role = ctx.request.body;
    await SysRoleService.save(role);
    ctx.body = ResultVo.success('success');
  }catch(err){
    //集中处理错误
    err.msg = "新增角色失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.get('/deleteOne',async(ctx)=>{
  try{
    let role = ctx.query;
    role = await SysRoleService.deleteOne(role.id);
    ctx.body = ResultVo.success('success',role);
  }catch(err){
    //集中处理错误
    err.msg = "删除角色失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.post('/deleteMany',async(ctx)=>{
  try{
    let param = ctx.request.body;
    console.log(param);
    param = await SysRoleService.deleteMany(param.ids);
    ctx.body = ResultVo.success('success',param);
  }catch(err){
    //集中处理错误
    err.msg = "批量删除角色失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.get('/findOne',async(ctx)=>{
  try{
    let role = ctx.query;
    role = await SysRoleService.findOne(role.id);
    //表格插件自动查找
    // let auths = await SysAuthService.findAll();
    // role.auths = auths;
    // console.log(auths);
    ctx.body = ResultVo.success('success',role);
  }catch(err){
    //集中处理错误
    err.msg = "查找角色失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.get('/findAll',async(ctx)=>{
  try{
    let data = await SysRoleService.findAll();
    ctx.body = ResultVo.success('success',data);
  }catch(error){
    ctx.body = ResultVo.error(-1,'获取全部角色数据失败!');
  }
});
router.get('/findByPage',async(ctx)=>{
  try{
    let page = ctx.query;
    console.log(page);
    let data = await SysRoleService.findByPage(page);
    ctx.body = data;
  }catch(error){
    ctx.body = ResultVo.error(-1,'获取分页角色数据失败!');
  }
});
module.exports = router;